<style>
	.upgrade-popup { padding: 40rpx; color: #737373; font-size: 28rpx; }
	.upgrade-popup ._title1 { margin-top: 20rpx; line-height: 60rpx; text-align: center; color: #3c3e42; font-size: 36rpx; font-weight: 700; }
	.upgrade-popup ._title2 { line-height: 50rpx; text-align: center; }
	.upgrade-popup ._title3 { padding: 28rpx 0 10rpx; color: #3c3e42; font-weight: 700; }
</style>

<template>
	<view>
		<uni-popup type="center" :mask-click="false" ref="popup">
			<view class="default-popup upgrade-popup">
				<view v-if="upgradeCloseVisible" class="default-popup_close" @click="$refs.popup.close()"></view>

				<view style="margin: 0 auto; width: 180rpx; height: 180rpx;">
					<image mode="scaleToFill" class="image" src="/static/upgrade.png"></image>
				</view>
				<view class="_title1">发现新版本</view>
				<view class="_title2">{{upgradeVersion}}</view>
				<view class="_title3">
					更新内容：
				</view>
				<view>
					{{upgradeRemark}}
				</view>

				<view style="padding-top: 50rpx;">
					<view class="theme-btn" @click="downloadUpgrade">立即升级</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryCheck: false,
				upgradeCloseVisible: true,
				upgradeVersion: '',
				upgradeRemark: '',
				upgradeUrl: '',
			}
		},
		created() {

		},
		methods: {
			check() {
				// #ifdef H5
				window.location.reload(true)
				return
				// #endif

				this.queryCheck = true
				this.autoCheck()
			},
			autoCheck() {
				let systemInfo = uni.getSystemInfoSync()
				this.$http.get('/appNoAuth/appVersion/chekcUpdate', {
					systemType: systemInfo.platform + '1',
					versionNo: systemInfo.appVersionCode,
				}).then(res => {
					if (!res && !res.data) {
						return
					}

					if (!res.data.isUpdate) {
						if (this.queryCheck) {
							this.queryCheck = false
							this.$common.toast('当前已是最新版本')
						}
						return
					}

					if (res.data.isUpdate) {
						// #ifdef APP-PLUS
						this.upgradeCloseVisible = res.data.mandatory ? true : false
						this.upgradeVersion = res.data.versionNo
						this.upgradeRemark = res.data.programDescription ? res.data.programDescription : '优化用户体验，推荐升级使用'
						this.upgradeUrl = res.data.uploadUrl
						this.$refs.popup.open()
						// #endif
					}
				})
			},
			downloadUpgrade() {
				this.$refs.popup.close()
				this.$common.toast('正在后台下载，请稍等...')

				let systemInfo = uni.getSystemInfoSync()
				if (systemInfo.platform == 'ios') {
					plus.runtime.launchApplication({
						action: `itms-apps://itunes.apple.com/cn/app/id${upgradeUrl}?mt=8`
					}, function(e) {

					})
					return
				}

				plus.downloader.createDownload(this.upgradeUrl, {}, (d, status) => {
					if (status != 200) {
						return
					}
					uni.showModal({
						content: '下载完成，现在安装？',
						showCancel: false,
						cancelColor: '#999999',
						cancelText: '下次提醒',
						confirmColor: '#aa8050',
						confirmText: '安装',
						success: (modal) => {
							if (modal.confirm) {
								this.installUpgrade(d.filename)
							}
						}
					})
				}).start()
			},
			installUpgrade(file) {
				plus.runtime.install(file, {}, () => {
					plus.runtime.restart()
				}, (e) => {
					plus.nativeUI.alert('安装失败，请重试')
				})
			},
		}
	}
</script>
